<?php

class ConvertHelper
{
    
    /**
     * Converts a german style decimal to a double value (remains type string)
     * 
     * @return string english decimal
     */
    static function germanDecimal($dec)
    {
        if (preg_match('#^\-?(0|[1-9]\d*),\d+$#', $dec))
        {
            return str_replace(',', '.', $dec);   
        }
        else
            return "0.00";
    }

    /**
     * Converts a english style decimal to a double value (remains type string)
     * 
     * @return string english decimal
     */
    static function doubleToGermanDecimal($dec)
    {
        if (preg_match('#^\-?(0|[1-9]\d*)\.\d+$#', $dec))
        {
            return str_replace('.', ',', $dec);   
        }
        else
            return "0,00";
    }
    
    static function toSQLDate ($date)
    {
    	if (preg_match('/\d{1,2}([^0-9])\d{1,2}[^0-9]\d{4}/', $date, $matches))
    	    return join('-', array_reverse(explode($matches[1], $date)));
    	else 
    	    return $date;
    }
    
    static function toGermanDate ($date, $skipYear = false)
    {
    	if (preg_match('/\d{4}\-\d{1,2}\-\d{1,2}/', $date, $matches)) {
            if ($skipYear) {
                $dateparts = explode('-', $date);
                array_shift($dateparts);
                return join('.', array_reverse($dateparts)).'.';
            } else
                return join('.', array_reverse(explode('-', $date)));
        } else {
    	    return $date;
        }
    }
    
    static function toTime ($date)
    {
    	if (preg_match('/\d{1,2}([^0-9])\d{1,2}([^0-9]\d{1,2})?/', $date, $matches))
    	    return join(':', explode($matches[1], $date));
    	else 
    	    return $date;
    }
    
    static function toDateTime ($date)
    {
    	if (preg_match('/\d{1,2}([^0-9])\d{1,2}[^0-9]\d{4} *\d{1,2}([^0-9])\d{1,2}([^0-9]\d{1,2})?/', $date, $matches)) {
    		// TODO Fertig machen - split an blanks und dann SQLDate und Time umwandeln mit den entspr. methoden
    	    return join(':', explode($matches[1], $date));
    	} else 
    	    return $date;
    }
    
}

?>